package com.situ.service.impl;
import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;


import com.situ.service.FOODService;
import com.situ.dao.FOODDao;
import com.situ.common.Assist;
import com.situ.entity.FOOD;

/**
 * FOOD的服务接口的实现类
 * 
 * @author 胡文 qq:986776
 *
 */
@Service
public class FOODServiceImpl implements FOODService {
	private final Logger LOG = LogManager.getLogger(this.getClass());

	@Autowired
	private FOODDao fOODDao;

/**
* 查询....
*/
    @Cacheable(value = "FOOD",key = "#root.methodName")
	@Override
	public List<FOOD> find(FOOD value) {

	//	List<FOOD> result = fOODDao.selectFOODByObj(value);
        List<FOOD> result = fOODDao.selectFOODByObj2(value);

		return result;
	}
    /**
* 根据id查询
*/
	@Override
    @CacheEvict(allEntries=true)
	public FOOD findOne(Integer id) {

		FOOD result = fOODDao.selectFOODById(id);

		return result;
	}

/**
* 添加不为空
*/
	@Override
    @CacheEvict(allEntries=true)
	public FOOD saveNotNull(FOOD value) {
		if (value == null) {
			if (LOG.isDebugEnabled()) {
				LOG.debug("执行将FOOD中属性值不为null的数据保存到数据库-->失败:对象不能为空");
			}
			return value;
		}
		if(value.getFname() == null || value.getImage() == null || value.getFdate() == null  ){
			if (LOG.isDebugEnabled()) {
				LOG.debug("执行将FOOD中属性值不为null的数据保存到数据库-->失败:存在不能为空的空值");
			}
			return value;
		}
		int result = fOODDao.insertNotNullFOOD(value);
		if (LOG.isDebugEnabled()) {
			LOG.debug("执行将FOOD中属性值不为null的数据保存到数据库-->结果:", result);
		}
		return value;
	}

    /**
* 修改不为空
*/
    @Override
    @CacheEvict(allEntries=true)
	public int updateNotNullById(FOOD value) {
		if (value == null) {
			if (LOG.isDebugEnabled()) {
				LOG.debug("执行通过FOOD的id更新FOOD中属性不为null的数据-->失败:对象为null");
			}
			return 0;
		}
		int result = fOODDao.updateNotNullFOODById(value);
		if (LOG.isDebugEnabled()) {
			LOG.debug("执行通过FOOD的id更新FOOD中属性不为null的数据-->结果:", result);
		}
		return result;
	}
    /**
* 删除
*/
	@Override
    @CacheEvict(allEntries=true)
	public int deleteById(Integer id) {
		if (id == null) {
			if (LOG.isDebugEnabled()) {
				LOG.debug("执行通过FOOD的id删除FOOD-->失败:id不能为空");
			}
			return 0;
		}
		int result = fOODDao.deleteFOODById(id);
		if (LOG.isDebugEnabled()) {
			LOG.debug("执行通过FOOD的id删除FOOD-->结果:", result);
		}
		return result;
	}



    @Override
    @Cacheable(value ="FOOD",key = "#p1+'--'+#p2") //Food::find
    public PageInfo find2(FOOD value, int pageNum, int pageSize) {

        PageHelper.startPage(pageNum, pageSize);
        List<FOOD> result = fOODDao.selectFOODByObj2(value);
        PageInfo pageInfo = new PageInfo<FOOD>(result,5);
        return pageInfo;

    }



}